﻿
Imports drawing.CommonInterfaces.UI
Imports drawing.CommonInterfaces.UI.interfaces


Namespace bozo
    Public Class bozo
        Implements IViewable, IShape, IClickable, ISelectable, IMovable, ITickable







#Region "members"
        Public startpoint As Point
        Dim ellipse1 As Ellipse
        Dim ellipse2 As Ellipse
        Dim ligne As New Line(New Pen(Color.Blue, 1))
        Public lignestepper As Integer = 0
        Public freeze As Boolean = False
        Public _width As Integer
        Public _heigth As Integer
        Dim _pen As Pen = Pens.Yellow
        Dim redPen As New Pen(Color.Red, 3)
        Dim bluePen As New Pen(Color.Blue, 3)
        Dim hovered As Boolean = False
        Dim _selected As Boolean = False
        Dim _id As Double
        Dim detector As BozoDetector
        Dim worldView As world.environement.World
        Dim _destination As Point
        Dim _origin As Point
        Dim _moving As Boolean = False
        Dim _pathlength As Double
        Dim _stepX As Integer
        Dim _stepY As Integer
        Dim _internalClock As Integer = 0


#End Region

#Region "constructors"

        Public Sub New(ByVal width As Integer, ByVal height As Integer, ByRef startpoint As Point, ByVal worldView As world.environement.World)
            Me.startpoint = startpoint
            Me._width = width
            Me._heigth = height
            Me.ellipse1 = New Ellipse(New Pen(Color.Red, 1), New Rectangle(X, Y, 0, 0))
            Me.ellipse2 = New Ellipse(New Pen(Color.Green, 1), New Rectangle(X + width, Y + height, 0, 0))
            ligne.point1 = New Point(X, Y)
            ligne.point2 = New Point(X + width, Y + height)
            detector = New BozoDetector(New Point(X + width / 2, Y + height / 2))
            Me.worldView = worldView
        End Sub
#End Region

#Region "iviewable"
        Public Sub draw(ByRef graph As System.Drawing.BufferedGraphics) Implements IViewable.draw

            ligne.draw(graph)
            ellipse1.draw(graph)
            ellipse2.draw(graph)
            Dim drawFont As New Font("Arial", 10)
            graph.Graphics.DrawString(Me.ID.ToString, drawFont, Brushes.White, X + 6, Y + height - 15)
            graph.Graphics.DrawString(detector.detected.Count, drawFont, Brushes.White, X + 6, Y + 6)

            ''graph.Graphics.DrawRectangles(Pens.Yellow, detector.sight)


            If Not selected Then
                If hovered Then
                    graph.Graphics.DrawRectangle(redPen, New Rectangle(X - 4, Y - 4, Me.width + 8, height + 8))
                End If

            Else
                graph.Graphics.DrawRectangle(bluePen, New Rectangle(X - 4, Y - 4, Me.width + 8, height + 8))
            End If
            ''graph.Graphics.DrawRectangle(pen, Me.getClippingRcangle())
        End Sub

        Public Sub resetSize(ByVal size As System.Drawing.Rectangle) Implements IViewable.resetSize

        End Sub
#End Region

#Region "thickable"
        Public Sub ligneThick()
            If lignestepper = 200 Then
                lignestepper = 0
            End If
            lignestepper += 1
            ligne.point1.X = Math.Abs(Math.Cos((lignestepper / 100) * Math.PI)) * width + X
            ligne.point1.Y = Math.Abs(Math.Sin((lignestepper / 100) * Math.PI)) * height + Y
        End Sub

        Public Sub ellipseThick()
            ellipse1.X = X
            ellipse1.Y = Y
            ellipse1.width = ligne.point1.X - startpoint.X
            ellipse1.height = ligne.point1.Y - startpoint.Y
            ellipse2.X = ligne.point1.X
            ellipse2.Y = ligne.point1.Y
            ellipse2.width = (width + startpoint.X) - ligne.point1.X
            ellipse2.height = (height + startpoint.Y) - ligne.point1.Y

        End Sub

        Public Sub thick() Implements ITickable.tick
            If Not freeze Then
                ligneThick()
                ellipseThick()
                flyTo(destination)
            End If
            detector.detect(Me.worldView)
        End Sub

        Public Property ID() As Double Implements ITickable.ID
            Get
                Return _id
            End Get
            Set(ByVal value As Double)
                _id = value
            End Set
        End Property

#End Region

#Region "ishape"


        Public Function getClippingRcangle() As System.Drawing.Rectangle Implements IShape.getClippingRcangle
            Return New Rectangle(startpoint.X, startpoint.Y, _width, _heigth)
        End Function

        Public Property height() As Integer Implements IShape.height
            Get
                Return Me.getClippingRcangle.Height
            End Get
            Set(ByVal value As Integer)

            End Set
        End Property

        Public Property pen() As System.Drawing.Pen Implements IShape.pen
            Get
                Return _pen
            End Get
            Set(ByVal value As System.Drawing.Pen)
                _pen = value
            End Set
        End Property

        Public Property width() As Integer Implements IShape.width
            Get
                Return Me.getClippingRcangle().Width
            End Get
            Set(ByVal value As Integer)

            End Set
        End Property

        Public Property X() As Integer Implements IShape.X
            Get
                Return startpoint.X
            End Get
            Set(ByVal value As Integer)


                startpoint.X = value
                ligne.point2 = New Point(X + width, Y + height)
                ligne.point1 = New Point(X, Y)
                detector.location = New Point(X + width / 2, Y + height / 2)
            End Set
        End Property

        Public Property Y() As Integer Implements IShape.Y
            Get
                Return startpoint.Y
            End Get
            Set(ByVal value As Integer)
                startpoint.Y = value
                ligne.point2 = New Point(X + width, Y + height)
                ligne.point1 = New Point(X, Y)
                detector.location = New Point(X + width / 2, Y + height / 2)
            End Set
        End Property
#End Region

#Region "IMovable"

        Public Property onTheMove() As Boolean Implements CommonInterfaces.UI.interfaces.IMovable.onTheMove
            Get
                Return _moving
            End Get
            Set(ByVal value As Boolean)
                _moving = value
            End Set
        End Property

        Public Property origin() As System.Drawing.Point Implements CommonInterfaces.UI.interfaces.IMovable.origin
            Get
                If origin = Nothing Then
                    Return New Point(X, Y)
                End If
                Return _origin
            End Get
            Set(ByVal value As System.Drawing.Point)
                _origin = value
            End Set
        End Property

        Public Sub flyTo(ByVal position As System.Drawing.Point) Implements CommonInterfaces.UI.interfaces.IMovable.flyTo
            If position = Nothing Then
                Return
            End If
            If position = New Point(X, Y) Then
                onTheMove = False
                origin = position
                _pathlength = 0
                position = Nothing
                Return
            End If

            Dim newposition As New Point(0, 0)
            onTheMove = True
            If _pathlength = 0 Then
                _pathlength = Math.Sqrt(Math.Pow(origin.X - position.X, 2) + Math.Pow(origin.Y - position.Y, 2))
                _stepX = (Math.Abs((origin.X - position.X)) / 100) * 1
                _stepY = (Math.Abs((origin.Y - position.Y)) / 100) * 1
            End If
            If _internalClock = 0 Then
                If X > position.X Then
                    newposition.X = X - _stepX
                ElseIf X < position.X Then
                    newposition.X = X + _stepX
                Else
                    newposition.X = X
                End If
                If Y > position.Y Then
                    newposition.Y = Y - _stepY
                ElseIf Y < position.Y Then
                    newposition.Y = Y + _stepY
                Else
                    newposition.Y = Y
                End If

                moveTo(newposition)
                _internalClock = 0
            Else
                _internalClock += 1
            End If






        End Sub

        Public Sub moveTo(ByVal position As System.Drawing.Point) Implements CommonInterfaces.UI.interfaces.IMovable.moveTo
            X = position.X
            Y = position.Y
            '' destination = New Point(X, Y)
        End Sub

        Public Property destination() As System.Drawing.Point Implements CommonInterfaces.UI.interfaces.IMovable.destination
            Get

                Return _destination
            End Get
            Set(ByVal value As System.Drawing.Point)
                origin = New Point(X, Y)
                _pathlength = 0
                _destination = value
            End Set
        End Property
#End Region

#Region "IClickable"

        Public Sub onClick() Implements CommonInterfaces.UI.interfaces.IClickable.onClick

            ''freeze = Not freeze

        End Sub

        Public Sub hover() Implements CommonInterfaces.UI.interfaces.IClickable.hover
            hovered = True
        End Sub

        Public Sub normal() Implements CommonInterfaces.UI.interfaces.IClickable.normal
            hovered = False
        End Sub




#End Region

#Region "ISelectable"
        Public Property selected() As Boolean Implements CommonInterfaces.UI.interfaces.ISelectable.selected
            Get
                Return _selected
            End Get
            Set(ByVal value As Boolean)
                _selected = value
            End Set
        End Property
#End Region






       



        
    End Class

End Namespace
